package com.chenyaowei.web.controller.payment;

import com.alibaba.fastjson2.JSONObject;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.payment.request.OrderReturnRequest;
import com.chenyaowei.payment.service.AlipayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "网站支付宝支付")
@RestController
@RequestMapping("/payment/alipay")
public class AlipayController extends BaseController {

    @Autowired
    private AlipayService alipayService;

    private final String SUCCESS = "Success";

    /**
     * 统一收单下单并支付页面接口，订单支付
     * https://open.alipay.com/api/apiDebug?frontProdCode=I1080300001000041203&backProdCode=I1011000100000000005
     */
    @ApiOperation("alipay.trade.page.pay(统一收单下单并支付页面接口)")
    @GetMapping("/websitePay/{orderSn}")
    public AjaxResult payOrder(@PathVariable("orderSn") String orderSn){
        String html = alipayService.payOrder(orderSn);
        return success(html);
    }

    /**
     * 支付异步回调
     * https://opendocs.alipay.com/open/270/105902?pathHash=d5cd617e
     * 校验成功后在response中返回success并继续商户自身业务处理，校验失败返回failure
     */
    @ApiOperation("支付异步回调")
    @PostMapping("/payed/notify")
    public String handleAlipayed(@RequestParam Map<String, String> params) {
        return alipayService.handleAlipayed(params);
    }

    /**
     * 统一收单交易关闭接口，取消订单的时候调用
     * https://opendocs.alipay.com/open/8dc9ebb3_alipay.trade.close?scene=common&pathHash=0c042d2b
     * 注意：针对⼆维码⽀付，只有经过扫码的订单才在⽀付宝端有交易记录。
     *      针对⽀付宝账号⽀付，只有经过登录的订单才在⽀付宝端有交易记录。
     */
    @ApiOperation("alipay.trade.close(统一收单交易关闭接口)")
    @GetMapping("/close/{orderSn}")
    public AjaxResult closeOrder(@PathVariable("orderSn") String orderSn) {
        String responseBody = alipayService.closeOrder(orderSn);
        JSONObject jsonObject = JSONObject.parse(responseBody);
        return SUCCESS.equals(jsonObject.get("msg")) ? success(jsonObject) : error(jsonObject.get("sub_msg").toString());
    }

    /**
     * 统一收单交易查询
     * https://opendocs.alipay.com/open/bff76748_alipay.trade.query?scene=23&pathHash=e3ddce1d
     */
    @ApiOperation("alipay.trade.query(统一收单交易查询)")
    @GetMapping("/query/{orderSn}")
    public AjaxResult queryOrder(@PathVariable("orderSn") String orderSn){
        String responseBody = alipayService.queryOrder(orderSn);
        JSONObject jsonObject = JSONObject.parse(responseBody);
        return SUCCESS.equals(jsonObject.get("msg")) ? success(jsonObject) : error(jsonObject.get("sub_msg").toString());
    }

    /**
     * 统一收单交易退款接口
     * https://opendocs.alipay.com/open/f60979b3_alipay.trade.refund?scene=common&pathHash=e4c921a7
     */
    @ApiOperation("alipay.trade.refund(统一收单交易退款接口)")
    @PutMapping("/refund}")
    public AjaxResult refundOrder(@RequestBody OrderReturnRequest orderReturnRequest){
        String responseBody = alipayService.refundOrder(orderReturnRequest);
        JSONObject jsonObject = JSONObject.parse(responseBody);
        return SUCCESS.equals(jsonObject.get("msg")) ? success(jsonObject) : error(jsonObject.get("sub_msg").toString());
    }

    /**
     * 统一收单交易退款查询
     * https://opendocs.alipay.com/open/357441a2_alipay.trade.fastpay.refund.query?scene=common&pathHash=01981dca
     */
    @ApiOperation("alipay.trade.fastpay.refund.query(统一收单交易退款查询)")
    @GetMapping("/queryReturn/{orderSn}")
    public AjaxResult queryReturnOrder(@PathVariable("orderSn") String orderSn){
        String responseBody = alipayService.queryReturnOrder(orderSn);
        JSONObject jsonObject = JSONObject.parse(responseBody);
        return SUCCESS.equals(jsonObject.get("msg")) ? success(jsonObject) : error(jsonObject.get("sub_msg").toString());
    }

}
